/* ===================================================================== */
/*                  Civilian Female - init script                        */
/* ===================================================================== */
_radius = 0.5;
_turn_radius = 10;
_run_radius = 4;
_useBIS_Move = true; // Use BIS movements (to avoid objects)
_BIS_radius = 1.6; // from this distance we have to do the work
_walk_only = false; // Can be overwrite using _agent setVariable ["WalkOnly",_walk_only];
/* ===================================================================== */
/*                    Dont change this point bellow                      */
/* ===================================================================== */
#include "Common\global.sqf";
if (!local _agent) exitWith {};
private["_agent"];
waitUntil {TES_AI_Ready};

/* ===================================================================== */

 _fnc_x_IsTargetInside = compile preprocessfile "TES\AI\Common\isInside.sqf";

 /* ===================================================================== */
_fnc_stop_action = {
	_moving = false;
	_done =  true;
	_agent playMoveNow "AmovPercMstpSnonWnonDnon";
	_move_type = "stop";
	_animDone = true;
	sleep 0.5;
	//_agent setPos _endpos;
	_agent setVariable ["_Status","Idle"];
	_agent setVariable ["_Command","None"];
};
_fnc_set_dir = {
	if (abs (_ang -_dir) > _turn_radius) then {
		if(_dist>0) then {
			_turn = if ( (((_ang -_dir) + 360) mod 360) < 180) then {_turn_radius} else {_turn_radius * (-1)};
			while {abs (((_ang -_dir) + 360) mod 360) > _turn_radius} do {
				_agent setDir _dir + _turn;
				if (_animDone) then {
					if (_turn >0) then {_agent playMove "AmovPercMstpSnonWnonDnon_AIturn90R";} else {
					_agent playMove "AmovPercMstpSnonWnonDnon_AIturn90L";};
					_animDone = false;
				};
				_dir = getDir _agent;
				sleep 0.1;
				//hintSilent format ["%1  <-> %2 ",_ang,_dir];
				//_agent playMoveNow "";
			};
			_agent setDir _ang;
			_agent playMoveNow "AmovPercMstpSnonWnonDnon";
			//_agent playMoveNow "";
		};
	} else {
			_agent setDir _ang;
			if (!_moving) then {_agent playMoveNow "AmovPercMstpSnonWnonDnon";_animDone = true;};
			//if (!_moving) then {_agent playMoveNow "";_animDone = true;};
		// sleep 0.1;
	};
};
/* ===================================================================== */
/* ===================================================================== */

_agent = (_this select 0);
_new_action=false;
_moving = false;
_move_type ="stop";
_pose = "stand";
_done = true;
_animDone = true;
_agent setVariable ["WalkOnly",_walk_only];
_dir = getDir _agent;
_pos = position _agent;
_mygroup = createGroup civilian;
_endpos = _pos;
//_agent addEventHandler ["killed", {(_this select 0) setVariable ['killer',(_this select 1)]}];
_agent setVariable ["_Command","None"];
_agent setVariable ["_Status","Idle"];
_agent setVariable ["_CurrentPose","Stand"];
_agent setBehaviour "SAFE";
_agent disableAI "ANIM";
_agent disableAI "MOVE";
_agent addEventHandler ["AnimDone", {_animDone = true;}];
_BIS_Moving = false;
_last_time = time;
_last_dist = 9999;
_isInside = false;
/* ===================================================================== */
_fnc_IsTargetInside = {
	_isInside = false;
	_nobj = nearestObject [_endpos,"Building"];
	if (!isNull _nobj) then {
		_isInside = [_endpos, _nobj] call _fnc_x_IsTargetInside;
	};
};

/**************************** MAIN LOOP **********************************/
while {alive _agent} do {
	
	_command =_agent getVariable "_Command";
	if ( _command != "None") then {
		switch (_command ) do {
			case ( "UpChair" ): { 
				_agent setVariable ["_Command","_UpChair"];
				_agent setVariable ["_Status","Busy"];
				//_agent disableAI "ANIM";
				_agent playMoveNow "AmovPercMstpSnonWnonDnon";
				//_agent switchMove "ActsPsitMstp_standUp";
				_agent setVariable ["_CurrentPose","Stand"];
				_agent setVariable ["_Status","Idle"];
				_agent setVariable ["_Command","None"];
			};
			case ( "SitChair" ): { 
				_agent setVariable ["_Command","_SitChair"];
				_agent setVariable ["_Status","Busy"];
				//_agent disableAI "ANIM";
				_agent playMoveNow "ActsPsitMstp_Sit";
				sleep 5;
				_agent setVariable ["_CurrentPose","SitChair"];
				_agent setVariable ["_Status","Idle"];
				_agent setVariable ["_Command","None"];
			};
			case ( "Goto" ): {
				_endpos = (_agent getVariable "_Parameters") select 0;
				call _fnc_IsTargetInside;
				#ifdef TES_AI_DEBUG
					if(_isInside) then {_agent globalChat format ["Target is Inside Building"];};
				#endif		
				_walk_only = _agent getVariable "WalkOnly";
				_agent setVariable ["_Command","_Goto"];
				_agent setVariable ["_Status","Busy"];
		
				_new_action=true;
			};
			case ( "SetDir" ): {
				_agent setVariable ["_Status","SetDir"];
				_agent setVariable ["_Status","Busy"];
				_agent disableAI "ANIM";
				_dist = 0.1;
				_dir = getDir _agent;
				_ang = (_agent getVariable "_Parameters") select 0;
				call _fnc_set_dir;
				sleep 0.5;
				_agent setVariable ["_Status","Idle"];
				_agent setVariable ["_Command","None"];
			};
		};
		//_agent setVariable ["_Command","None"];
	};
	if (_new_action) then {
		_agent disableAI "ANIM"; _done = false; _new_action = false;
	};
	if (!_done) then {
		_inipos = getPos _agent;
		_dir = getDir _agent;
		_dist = sqrt(((_endpos select 0) - (_inipos select 0))^2 + ((_endpos select 1) - (_inipos select 1))^2);
		_ang = ((_endpos select 0) - (_inipos select 0)) atan2 ((_endpos select 1) - (_inipos select 1));
		//hintSilent format ["(%7) %1-%2 <-> %3 - %4 (%5,%6)",round (_pos select 0),round(_pos select 1),round (_endpos select 0),round (_endpos select 1),_ang,_dir,_dist];
		if (_ang < 0) then {_ang=_ang + 360};
		//hintSilent format["%1\n%2",str(_inipos),str(_dist)];
		if (_useBIS_Move and !_BIS_Moving and (_dist > _run_radius)) then {
			#ifdef TES_AI_DEBUG
				_agent globalChat format ["Start BIS"];
			#endif
			_agent doMove _endpos;
			if (_walk_only) then { _agent setSpeedMode "LIMITED";} else { _agent setSpeedMode "NORMAL";};
			_BIS_Moving = true;
			_agent enableAI "ANIM";
			_agent enableAI "MOVE";
			_last_time = time;
			_last_dist = _dist;
		} else {
			if (_useBIS_Move and _BIS_Moving) then {
				if (_dist < _BIS_radius) then {
					#ifdef TES_AI_DEBUG
						_agent globalChat format ["Stoping BIS"];
					#endif
					_BIS_Moving=false;
					_agent disableAI "ANIM";
					_agent disableAI "MOVE";
				} else {
					// hintSilent format["%1\n%2\n%3\n%4\n%5",_last_dist,_dist,_last_time,time,time - _last_time];
					if (_last_dist == _dist) then {
						if (time - _last_time > 5) then {
							#ifdef TES_AI_DEBUG
								_agent globalChat format ["BIS Hang"];
							#endif
							_BIS_Moving=false;
							_agent disableAI "ANIM";
							_agent disableAI "MOVE";
						};
					} else {
						_last_time = time;
					};
					_last_dist = _dist;
				};
			} else {
				switch (true) do {
					case ( _moving ): { 

						if (_dist <= _radius) then {
							call _fnc_stop_action;
						} else {
								if (_dist > _run_radius and !_walk_only) then {
									if (_animDone or _move_type != "jogging") then {_agent playMoveNow "AmovPerc_joggingSlowerS0"; move_type = "jogging";_animDone = false;};
								} else {
									if (_animDone or _move_type != "walk") then { _agent playMoveNow "AmovPercMwlkSnonWnonDf_A"; move_type = "walk";_animDone = false;};	
								};
						};
						if (abs (_ang -_dir) > 5) then {
							_agent setDir _dir + 5;
						} else {_agent setDir _ang;};
					};
					default {
						call _fnc_set_dir;
						if (_dist > _radius and !_moving) then {
							if (_dist > _run_radius and !_walk_only) then {
								_agent playMoveNow "AmovPerc_joggingSlowerS0";	
							} else {
								_agent playMoveNow "AmovPercMwlkSnonWnonDf_A";		
							};
							_animDone = false;
							_moving = true;
							//["AmovPerc_joggingSlowerS0"]
						};
						if (_dist <= _radius and !_moving ) then {
							call _fnc_stop_action;
						};
					};
				};
			};
		};
		if (_dist > 5 and ! _useBIS_Move) then { sleep 1; };// else {sleep 0.1;};
	} else { sleep 0.5; };
};
// hintSilent format ["(%7) %1-%2 <-> %3 - %4 (%5,%6)",round (_pos select 0),round(_pos select 1),round (_endpos select 0),round (_endpos select 1),_ang,_dir,_dist];
//_agent enableAI "ANIM";
//sleep 1;
//cutText ["Civilian Died","PLAIN",1];
//_killer = (_agent getVariable "killer");

